package com.bbs.crawl;

import java.sql.Statement;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bbs.bean.BBSOfBitAuto;
import com.bbs.dao.BBSDaoOfBitAuto;
import com.car.util.DocUtil;

public class BBSCrawlOfBitAuto {
	static Logger logger = Logger.getLogger(BBSCrawlOfBitAuto.class.getName());
	private BBSDaoOfBitAuto dao = new BBSDaoOfBitAuto();

	// 入口
	public void getUrls(String urlSpell) {
		String url = "http://baa.bitauto.com/" + urlSpell + "/index-all-all-1-0.html";
		int page = getBBSPages(url);
		if (page == 0) {
			return;
		}
		logger.info(urlSpell + " Start！");
		dao.createTable(urlSpell); 
		for (int i = 1; i < page + 1; i++) {
			String bbsPageUrl = "http://baa.bitauto.com/" + urlSpell + "/index-all-all-" + i + "-0.html";
			Document contentPage = DocUtil.getDoc(bbsPageUrl);
			Elements liPage = null;
			try {
				liPage = contentPage.select("div.postslist_xh li.bt a");
			} catch (Exception e1) {
				break;
			}
			for (Element e : liPage) {
				String bbsUrl = e.attr("href");
				startCrawl(bbsUrl);
			}
		}
		logger.info(urlSpell + " Finish！");
		dao.updateBBSTime("bitauto_ltgxsj", urlSpell);
	}

	public int getBBSPages(String url) {
		int page = 1;
		try {
			Document doc = DocUtil.getDoc(url);
			Elements pages = doc.select("div.the_pages a");
			int pageSize = pages.size();
			Element maxPage = pages.get(pageSize - 2);
			page = Integer.parseInt(maxPage.text());
		} catch (Exception e) {
			logger.error(url + "没有论坛，直接略过");
			page = 0;
		}
		return page;
	}

	public void startCrawl(String url) {
		Document doc = DocUtil.getDoc(url);
		try {
			int bbs_bid = Integer.parseInt(url.split("-")[1].split(".html")[0]);
			String bbs_bt = doc.select("div.title_box h1").get(0).text();
			int bbs_sfwd = url.contains("ask") ? 1 : 0;
			int bbs_sfjh = doc.select("div.jinghuazhang") == null ? 0 : 1;
			String bbs_nr = doc.select("div.post_width").text();
			String bbs_ftsj = doc.select("div.time_box>span").get(0).text().substring(3);
			String hit = doc.select("div.title_box span").text();
			int bbs_djs = Integer.parseInt(hit.split("/")[1]);
			int bbs_hfs = Integer.parseInt(hit.split("/")[0]);
			String yhid = doc.select("div.user_name a.mingzi").get(0).attr("href").split("u")[1];
			int bbs_yhid = Integer.parseInt(yhid.substring(0, yhid.length() - 1));
			Elements userInfo = doc.select("div.user_info li");
			String bbs_yhdj = userInfo.get(0).select("span").text();
			int bbs_tzs = Integer.parseInt(userInfo.get(1).select("span a").text());
			int bbs_tzjh = Integer.parseInt(userInfo.get(1).select("span").text().split("\\(")[1].split("精华")[0]);
			String bbs_dq = userInfo.get(2).select("span").text();
			String bbs_zc = userInfo.get(4).select("span").text();
			BBSOfBitAuto b = new BBSOfBitAuto(bbs_bid, bbs_bt, bbs_sfjh, bbs_sfwd, bbs_nr, bbs_ftsj, bbs_hfs, bbs_djs,
					bbs_yhid, bbs_yhdj, bbs_tzs, bbs_tzjh, bbs_dq, bbs_zc);
			System.out.println(b);
			dao.insertBBS(b);
		} catch (Exception e) {
			logger.error("该BBS被删除");
		}

	}

	public static void main(String[] args) {
		BBSCrawlOfBitAuto b = new BBSCrawlOfBitAuto();
		b.getUrls("aodi100");
	}
}
